/**
 * @Author: Liu, Yunfeng
 * @DateTime: 2021/4/7 20:59
 * @Description: 搜索旋转排序数组 II
 */
public class search {
    public static boolean search(int[] nums, int target) {
        int midIndex = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < nums[midIndex]) {
                break;
            }
            midIndex = i;
        }
        if (target == nums[nums.length - 1]) {
            return true;
        }
        return (binarySearch(nums, midIndex + 1, nums.length - 1, target) > -1) ||
                (binarySearch(nums, 0, midIndex, target) > -1);
    }

    private static int binarySearch(int[] nums, int start, int end, int target) {
        if (start == end) {
            return target == nums[start] ? start : -1;
        }
        int mid = 0;
        while (start <= end) {
            mid = (start + end) / 2;
            if (target == nums[mid]) {
                return mid;
            }
            if (target > nums[mid]) {
                start = mid + 1;
            } else {
                end = mid - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] nums = {1,2,3};
        int target = 2;
        System.out.println(search(nums, target));
    }
}
